package com.feihua.starter.comm.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @Auther: wzn
 * @Date: 2018/7/23 09:30
 * @Description: 复制工具类
 */
public class CopyUtils {
    private static Logger logger = LoggerFactory.getLogger(CopyUtils.class);
    public static Serializable copyObject(Serializable object) {
        // 字节数组输出流
        ByteArrayOutputStream baos = null;
        // 对象组输出流
        ObjectOutputStream oos = null;

        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        // 序列化接口类
        Serializable readObject = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);

            oos.writeObject(object);

            byte[] byteArray = baos.toByteArray();

            bais = new ByteArrayInputStream(byteArray);
            ois = new ObjectInputStream(bais);

            readObject = (Serializable) ois.readObject();
        } catch (ClassNotFoundException e) {
            logger.error(e.getMessage(),e);
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(),e);
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(),e);
                }
            }
        }
        return readObject;
    }
}
